package com.supor.aiot.food.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.food.R;
import com.supor.aiot.food.adapter.SelectDeviceAdapter;
import com.supor.aiot.food.bean.BindDeviceBean;

import java.util.ArrayList;
import java.util.List;

public class SelectDevicePopwindow extends PopupWindow {
    private List<BindDeviceBean> mDeviceList;
    private RecyclerView deviceRv;
    private View  mMenuView ;
    private SelectDeviceAdapter selectDeviceAdapter;
    private Context mContext;
    private OnPopDeviceListener popDeviceListener;

    public SelectDevicePopwindow(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.select_device_popwindow, null);
        deviceRv = mMenuView.findViewById(R.id.rv_select_device);
        setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体的高
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        //setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener((v, event) -> {
            // 防止NPE
            if (mMenuView.findViewById(R.id.rl_select_device) != null) {
                int height = mMenuView.findViewById(R.id.rl_select_device).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
            }
            return true;
        });
    }

    public void setDeviceData(List<BindDeviceBean> list){
        if (list!=null&&list.size()>0){
            this.mDeviceList=new ArrayList<>();
            BindDeviceBean devcieBean = new BindDeviceBean();
            devcieBean.setModelTypeName(mContext.getResources().getString(R.string.total_device_recipes));
            devcieBean.setModelTypeId("");
            mDeviceList.add(devcieBean);
            mDeviceList.addAll(list);
        }
        selectDeviceAdapter = new SelectDeviceAdapter(mDeviceList, mContext, (position, state) -> {
            if (popDeviceListener!=null)popDeviceListener.onDeviceSelected(position,mDeviceList.get(position).getModelId(), mDeviceList.get(position).getModelTypeName());
            dismiss();
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        deviceRv.setLayoutManager(layoutManager);
        deviceRv.setAdapter(selectDeviceAdapter);
    }

    public void setPopDeviceListener(OnPopDeviceListener popDeviceListener) {
        this.popDeviceListener = popDeviceListener;
    }

    public void setPopDevicePosition(int position) {
        if (selectDeviceAdapter!=null){
            selectDeviceAdapter.setPosition(position);
            Log.e("nail","selectDeviceAdapter = "+position);
        }else {
            Log.e("nail","selectDeviceAdapter = null");
        }
    }

    public interface OnPopDeviceListener{
        void onDeviceSelected(int position,String typeID,String deviceName);
    }

}
